﻿var width=10;//沙盘宽度
var height=10;//沙盘高度
var typeNum=7;
var sandbox=new Array();
var selectedCell=[{isSelect:0,height:-1,width:-1},{isSelect:0,height:-1,width:-1}];
var score=0;//得分
var time=0;//初始时间
var timeStatic=10;
$(document).ready(function(){ 
    //init var sandbox
	for(var i=0;i<height;i++){
	  sandbox[i]=new Array();
	}	
    //Start Game
	$("#start").click(function(){
	  //alert("Game start!~");
	  console.log("Log:Game start");
	  showSandbox()	 
	  initLayout();	  
      timedCount();	  
	});	
});

//初始化sandbox
function initLayout(){
  initTable();
  for(var indexH=0;indexH<height;indexH++){
     for(var indexW=0;indexW<width;indexW++){
	    initCell(indexH,indexW);
	 }
  }
  var isCleared=false;//如果有需要清除的cell，则返回false,如果没有需要清除的cell则返回true
  //while(!isCleared){
	isCleared=allSearchClear();
	while(!isCleared){
	  allFilled();
	  isCleared=allSearchClear();
	}
 // }
}
//初始化cell
function initCell(indexH,indexW){
  var indexI=parseInt(Math.random()*typeNum);    
  sandbox[indexH][indexW]=indexI;    
  $("#tr_"+indexH).children().eq(indexW).html("<button style=\"background-color:transparent\"id=\"id_"+indexH+"_"+indexW+"\" onClick=\"selectCell("+indexH+","+indexW+")\">"+indexI+"</button>");
}
//初始化table
function initTable(){
  $("#sandbox").html("");//Clear table content
  for(var indexH=0;indexH<height;indexH++){
     $("#sandbox").append("<tr id=\"tr_"+indexH+"\"></tr>");
     for(var indexW=0;indexW<width;indexW++){
	    $("#tr_"+indexH).append("<td id=\"td_"+indexW+"\"></td>");
	 }
  }    
}
//显示界面
function showSandbox(){
  $("#sandboxDiv").show();
  $("#infoDiv").show();
  $("#startDiv").hide();
  score=0;
  time=timeStatic;
  $("#time").html(time);
  $("#score").html(score); 
}
function hideSandbox(){
  $("#sandboxDiv").hide();
  $("#infoDiv").hide();
  $("#startDiv").show();
  score=0;
  time=59;
  $("#time").html(time);
  $("#score").html(score); 
}
//选择cell
function selectCell(indexH,indexW){
if(selectedCell[0].isSelect==0){//未选择过任何Cell
  selectedCell[0].isSelect=1;
  selectedCell[0].height=indexH;
  selectedCell[0].width=indexW;
  $("#id_"+indexH+"_"+indexW).css("background-color","red"); 
}else{
  if(selectedCell[0].height==indexH&&selectedCell[0].width==indexW){//取消已选择的Cell
     $("#id_"+indexH+"_"+indexW).css("background-color","transparent"); 
	 selectedCell[0].isSelect=0;
  }else{
     if((Math.abs(selectedCell[0].height-indexH)==0&&Math.abs(selectedCell[0].width-indexW)==1)||(Math.abs(selectedCell[0].height-indexH)==1&&Math.abs(selectedCell[0].width-indexW)==0)){//符合选择条件，进行消除
	    //补充到isSelectedCell
	    selectedCell[1].isSelect=1;
	    selectedCell[1].height=indexH;
	    selectedCell[1].width=indexW;
	    $("#id_"+indexH+"_"+indexW).css("background-color","red"); 
		//交换sandbox
		var temp=sandbox[selectedCell[0].height][selectedCell[0].width];
		sandbox[selectedCell[0].height][selectedCell[0].width]=sandbox[selectedCell[1].height][selectedCell[1].width];
		sandbox[selectedCell[1].height][selectedCell[1].width]=temp;
		//交换显示
		$("#tr_"+selectedCell[0].height).children().eq(selectedCell[0].width).html("<button style=\"background-color:transparent\"id=\"id_"+selectedCell[0].height+"_"+selectedCell[0].width+"\" onClick=\"selectCell("+selectedCell[0].height+","+selectedCell[0].width+")\">"+sandbox[selectedCell[0].height][selectedCell[0].width]+"</button>");
		$("#tr_"+selectedCell[1].height).children().eq(selectedCell[1].width).html("<button style=\"background-color:transparent\"id=\"id_"+selectedCell[1].height+"_"+selectedCell[1].width+"\" onClick=\"selectCell("+selectedCell[1].height+","+selectedCell[1].width+")\">"+sandbox[selectedCell[1].height][selectedCell[1].width]+"</button>");
		//清除
	    var isCleared=allSearchClear();
		var isClearedTMP=isCleared;
		while(!isCleared){
		  allFilled();
		  isCleared=allSearchClear();
		}
		console.log("isCleared"+isCleared);
		if(isClearedTMP){
		    temp=sandbox[selectedCell[0].height][selectedCell[0].width];
			sandbox[selectedCell[0].height][selectedCell[0].width]=sandbox[selectedCell[1].height][selectedCell[1].width];
			sandbox[selectedCell[1].height][selectedCell[1].width]=temp;
			$("#tr_"+selectedCell[0].height).children().eq(selectedCell[0].width).html("<button style=\"background-color:transparent\"id=\"id_"+selectedCell[0].height+"_"+selectedCell[0].width+"\" onClick=\"selectCell("+selectedCell[0].height+","+selectedCell[0].width+")\">"+sandbox[selectedCell[0].height][selectedCell[0].width]+"</button>");
			$("#tr_"+selectedCell[1].height).children().eq(selectedCell[1].width).html("<button style=\"background-color:transparent\"id=\"id_"+selectedCell[1].height+"_"+selectedCell[1].width+"\" onClick=\"selectCell("+selectedCell[1].height+","+selectedCell[1].width+")\">"+sandbox[selectedCell[1].height][selectedCell[1].width]+"</button>");			
		}
		selectedCell[0].isSelect=0;
		selectedCell[1].isSelect=0;
	}else{//不相邻的Cell
	   $("#id_"+selectedCell[0].height+"_"+selectedCell[0].width).css("background-color","transparent");
	   selectedCell[0].isSelect=1;
	   selectedCell[0].height=indexH;
	   selectedCell[0].width=indexW;
	   $("#id_"+indexH+"_"+indexW).css("background-color","red");
	}
  }
 }
}
/*如果有需要清除的cell，则返回false,如果没有需要清除的cell则返回true*/
function allSearchClear(){
  console.log("All Search Clear");
  var isCleared=false;
  var clearList=new Array();
  var cursor=true;
  var count=1;
  //横向判断
  for(var indexH=0;indexH<window.height;indexH++){
     for(var indexW=0;indexW<window.width;indexW++){
       if(indexW+1<window.width){
	     if(sandbox[indexH][indexW]==sandbox[indexH][indexW+1]){		   
		   count++;
		 }else{
		   if(count>=3){
		      for(var j=0;j<count;j++){
			    clearList.push({height:indexH,width:indexW-j});
			  }			  
		   }
		   count=1;
		 }		 
	   }else{
	      if(count>=3){
		      for(var j=0;j<count;j++){
			    clearList.push({height:indexH,width:indexW-j});
			  }			  
		   }
		   count=1;
       }	   
	 }
  }
  //纵向判断
  for(var indexW=0;indexW<window.width;indexW++){
     for(var indexH=0;indexH<window.height;indexH++){
       if(indexH+1<window.height){
	     if(sandbox[indexH][indexW]==sandbox[indexH+1][indexW]){		   
		   count++;
		 }else{
		   if(count>=3){
		      for(var j=0;j<count;j++){
			    clearList.push({height:indexH-j,width:indexW});
			  }			  
		   }
		   count=1;
		 }		 
	   }else{
	       if(count>=3){
			  for(var j=0;j<count;j++){
				clearList.push({height:indexH-j,width:indexW});
			  }			  
		   }
		   count=1;
       }	   
	 }
  }
  if(clearList.length!=0){	   
	  //Clear cell
	  for(var i=0;i<clearList.length;i++){
		 var height=clearList[i].height;
		 var width=clearList[i].width;		 
		 var value=window.sandbox[height][width];
		 
		 if(value!=-1){
		   window.sandbox[height][width]=-1;
		   $("#tr_"+height).children().eq(width).html("");		   
		 }
	  }
	  score=score+clearList.length; 
	  $("#score").html(score);
	  console.log("Score:"+score);
	   clearList=[];
	   return false;
  }else{
	   return true;
  }
  
}
function allFilled(){
  console.log("All Filled");
  for(var indexW=0;indexW<width;indexW++){
    var tempList=new Array();
    for(var indexH=height-1;indexH>=0;indexH--){
	  if(sandbox[indexH][indexW]!=-1){
	     tempList.push(sandbox[indexH][indexW]);
	  }
	}
    while(tempList.length<height){
      tempList.push(parseInt(Math.random()*typeNum)); 
	}	
	
	for(var indexH=height-1;indexH>=0;indexH--){
	   sandbox[indexH][indexW]=tempList[height-1-indexH];
	   $("#tr_"+indexH).children().eq(indexW).html("<button style=\"background-color:transparent\"id=\"id_"+indexH+"_"+indexW+"\" onClick=\"selectCell("+indexH+","+indexW+")\">"+sandbox[indexH][indexW]+"</button>");
	}
  }  
}

function timedCount()
{ 
  if(time==0){
    clearTimeout("timedCount()");
	alert("Game Over! Your score:"+score);
	hideSandbox();
  }else{
      time--;
	  $("#time").html(time);
	  setTimeout("timedCount()",1000);
  }  
}